package com.secf.service.action.hpy.mc.scjcjd.test;

import com.secf.service.action.hpy.mc.scjcjd.test.MultiBuffer;

/**
 * @Author luoYong
 * @Date 2024-08-09 16:43
 */
public class MultiProducerConsumerTest {
    public static void main(String[] args) {
        MultiBuffer buffer = new MultiBuffer();

        Runnable producerTask = () -> {
            try {
                for (int i = 0; i < 10; i++) {
                    buffer.produce(i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        };

        Runnable consumerTask = () -> {
            try {
                for (int i = 0; i < 10; i++) {
                    buffer.consume();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        };

        Thread[] producers = new Thread[3];
        Thread[] consumers = new Thread[3];

        for (int i = 0; i < 3; i++) {
            producers[i] = new Thread(producerTask);
            consumers[i] = new Thread(consumerTask);
        }

        for (Thread producer : producers) {
            producer.start();
        }

        for (Thread consumer : consumers) {
            consumer.start();
        }
    }
}